//
//  StatusOriginalViewFrame.m
//  Weibo
//
//  Created by Anasue on 9/16/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "StatusOriginalViewFrame.h"
#import "Status.h"
#import "User.h"
#import "StatusPhotosView.h"

@implementation StatusOriginalViewFrame

- (void)setStatus:(Status *)status
{
    _status = status;
    
    //1.设置头像frame
    CGFloat iconX = StatusCellInSet;
    CGFloat iconY = StatusCellInSet;
    CGFloat iconW = 35;
    CGFloat iconH = 35;
    self.iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    //2.设置昵称frame
    CGFloat nameX = CGRectGetMaxX(self.iconFrame) + StatusCellInSet;
    CGFloat nameY = iconY;
    NSMutableDictionary *nameAttr = [NSMutableDictionary dictionary];
    nameAttr[NSFontAttributeName] =StatusOriginalNameFont;
    CGSize nameSize = [status.user.name sizeWithAttributes:nameAttr];
    self.nameFrame = (CGRect){{nameX,nameY},nameSize};
    
    //设置会员
    if (status.user.isVip) {
        CGFloat vipX = CGRectGetMaxX(self.nameFrame) + StatusCellInSet;
        CGFloat vipY = nameY;
        CGFloat vipH = nameSize.height - 4;
        CGFloat vipW = vipH;
        self.vipFrame = CGRectMake(vipX, vipY, vipW, vipH);
    }
    
    
//    //3.设置时间frame
//    CGFloat timeX = nameX;
//    CGFloat timeY = CGRectGetMaxY(self.nameFrame) + StatusCellInSet * 0.5;
//    NSMutableDictionary *timeAttr = [NSMutableDictionary dictionary];
//    timeAttr[NSFontAttributeName] =StatusOriginalTimeFont;
//    CGSize timeSize = [status.created_at sizeWithAttributes:timeAttr];
//    self.timeFrame = (CGRect){{timeX,timeY},timeSize};

//    //4.设置来源frame
//    CGFloat sourceX = CGRectGetMaxX(self.timeFrame) + StatusCellInSet * 0.5;
//    CGFloat sourceY = timeY;
//    NSMutableDictionary *sourceAttr = [NSMutableDictionary dictionary];
//    sourceAttr[NSFontAttributeName] =StatusOriginalSourceFont;
//    CGSize sourceSize = [status.source sizeWithAttributes:sourceAttr];
//    self.sourceFrame = (CGRect){{sourceX,sourceY},sourceSize};

    //5.设置正文frame
    CGFloat textX = iconX;
    CGFloat textY = CGRectGetMaxY(self.iconFrame) + StatusCellInSet *0.5;
    CGFloat textW = ScreenW - 2 *StatusCellInSet;
    CGSize maxSize =CGSizeMake(textW, MAXFLOAT);
//    CGSize textSize = [status.text sizeWithFont:StatusOriginalTextFont constrainedToSize:maxSize];
    CGSize textSize = [status.attributedText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    
//    NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
//    textAttr[NSFontAttributeName] =StatusOriginalTextFont;
//    CGSize; textSize = [boundingRectWithSize:maxSize options:NSStringDrawingUsesDeviceMetrics attributes:textAttr context:status.text];
    self.textFrame = (CGRect){{textX,textY},textSize};
    
    //6.设置配图frame
    CGFloat h = 0;
    if (status.pic_urls.count) {
        CGFloat photosX = iconX;
        CGFloat photosY = CGRectGetMaxY(self.textFrame) + StatusCellInSet * 0.5;
        CGSize photosSize = [StatusPhotosView sizeWithPhotosCount:(int)status.pic_urls.count];
        self.photosFrame = (CGRect){{photosX,photosY},photosSize};
//        Log(@"%@",NSStringFromCGRect(self.photosFrame));
        h = CGRectGetMaxY(self.photosFrame) + StatusCellInSet;
    }else{
        h = CGRectGetMaxY(self.textFrame) + StatusCellInSet;
    }
    

    //6.设置自己frame
    CGFloat x = 0;
    CGFloat y = 10;
    CGFloat w = ScreenW;
//    CGFloat h = CGRectGetMaxY(self.textFrame);
    self.frame = CGRectMake(x, y, w, h);

}

@end
